<?php

class upload_file {
	public $upfile_type; //上传文件类型
	public $upfile_size; //上传文件大小
	public $upfile_name; //上传文件名称
	public $upfile; //上传文件
	public $base_directory; //上传根目录
	public $extention_list; //允许上传的扩展名
	public $file_path; //上传后文件的完整路径
	public $size; //最大上传大小
	

	public $isDefault_fileName = false; //false用随机码作为上传后的文件名，true用上传的文件名作为上传后的文件名
	public $is_have_subdir = false; //是否添加子目录 false直接上传到根目录$base_directory，true在根目录下建$flash_directory子目录
	public $flash_directory; //子目录
	public $file_last_name;
	
	/**
	 * upload_file 构造函数
	 * Enter description here ...
	 * @param unknown_type $file 上传文件
	 * @param unknown_type $filename 上传的文件名
	 * @param unknown_type $filesize 上传文件大小
	 * @param unknown_type $baseDir 上传的根目录
	 * @param unknown_type $extention 运行上传的文件类型（扩展名：如gif|jpg|xml|doc|zip等等，用|隔开）
	 * @param unknown_type $fileSize 最大上传文件大小 以M为单位
	 */
	public function __construct($file, $filename, $filesize, $baseDir, $extention_list, $fileSize) {
		$this->upfile = $file;
		$this->upfile_name = $filename;
		$this->upfile_size = $filesize;
		$this->base_directory = $baseDir;
		$this->extention_list = $extention_list;
		$this->size = $fileSize;
	}
	
	/**
	 * 错误处理
	 * Enter description here ...
	 * @param unknown_type $errstr
	 */
	private function showerror($errstr = "未知错误！") {
		echo "<script language=javascript>alert('$errstr');location='javascript:history.go(-1);';</script>";
		exit ();
	}
	
	/**
	 * 是否用原始文件名 作为上传后的文件名称
	 * Enter description here ...
	 * @param unknown_type $is_default_filename
	 */
	public function set_is_default_filename($is_default_filename = false) {
		$this->isDefault_fileName = $is_default_filename;
	}
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $is_have_subdir
	 * @param unknown_type $subdir
	 */
	public function set_have_subdirectory($is_have_subdir = false, $subdir) {
		$this->is_have_subdir = $is_have_subdir;
		if ($this->is_have_subdir) {
			$this->flash_directory = $this->base_directory . "/" . $subdir;
		}
	}
	
	/**
	 * 验证文件类型
	 * Enter description here ...
	 */
	private function check_extention() {
		$this->extention = preg_replace ( '/.*\.(.*[^\.].*)*/iU', '\\1', $this->upfile_name );
		$ext = explode ( "|", $this->extention_list );
		$check = false;
		//遍历数组;
		for($i = 0; each ( $ext ); $i ++) {
			//比较文件扩展名是否与默认允许的扩展名相符;
			if ($ext [$i] == strtolower ( $this->extention )) {
				$check = true; //相符则标记;
				break;
			}
		}
		if (! $check) {
			$this->showerror ( "正确的扩展名必须为" . $this->extention_list . "其中的一种！" );
		}
	}
	/**
	 * 验证文件大小
	 * Enter description here ...
	 */
	private function check_size() {
		if ($this->upfile_size > round ( $this->size * 1024 * 1024 )) {
			$this->showerror ( "上传文件不得超过" . $this->size . "MB" );
		}
	}
	
	/**
	 * 创建根目录
	 * Enter description here ...
	 */
	private function mk_base_dir() {
		if (! file_exists ( $this->base_directory )) { //检测根目录是否存在;
			@mkdir ( $this->base_directory, 0777 ); //不存在则创建;
		}
	}
	/**
	 * 创建子目录
	 * Enter description here ...
	 */
	private function mk_file_dir() {
		if ($this->is_have_subdir) {
			if (! file_exists ( $this->flash_directory )) {
				@mkdir ( $this->flash_directory, 0777 ); //不存在则创建;
			}
		}
	}
	
	/**
	 * 获取最终生成的文件名称
	 * Enter description here ...
	 */
	private function get_file_name() {
		if ($this->isDefault_fileName) {
			$this->file_last_name = $this->upfile_name;
		} else {
			$this->file_last_name = uniqid ( "upload" ) . "." . $this->extention;
		}
	}
	
	/**
	 * 生成完整的文件路径
	 * Enter description here ...
	 */
	private function set_file_path() {
		$this->get_file_name ();
		if ($this->is_have_subdir) {
			$this->file_path = $this->flash_directory . "/" . $this->file_last_name; //生成文件完整访问路径;
		} else {
			$this->file_path = $this->base_directory . "/" . $this->file_last_name; //生成文件完整访问路径;
		}
	
	}
	
	/**
	 * copy文件
	 * Enter description here ...
	 */
	private function move_file() {
		$this->set_file_path();
		if (file_exists ( $this->file_path )) {
			$this->showerror ( $this->file_last_name . "--文件名已存在" );
		} else {
			move_uploaded_file ( $this->upfile, $this->file_path );
		}
	}
	
	/**
	 * 上传文件
	 * Enter description here ...
	 */
	public function upload() {
		$this->check_extention();
		$this->check_size();
		$this->mk_base_dir();
		$this->mk_file_dir();
		$this->move_file();
	}

}